package com.china08.yxyapi.api.manage.sch.office.cookbook;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.cookbook.CookBook;
import com.china08.yxyapi.entity.mg.cookbook.CookBook.Meal;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.cookbook.YxySchManageCookbookRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 每周食谱--管理员--编辑
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class YxySchManagerCookbookUpdController extends YxyApiBaseController {
    @Autowired
    private YxySchManageCookbookRepository yxySchManageCookbookRepository;

    @RequestMapping(value = "/api_yw/manage/sch/offic/cookBook/YxySchManageCookbookUpd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        Integer weekId = reqModel.weekId;
        CookBook cookBook = yxySchManageCookbookRepository.findBySchoolIdAndWeekId(schoolId, weekId);
        if (cookBook == null) {
            cookBook = new CookBook();
            System.out.println("123");
        }
        cookBook.setWeekId(weekId);
        cookBook.setWeekName(DateUtils.Week1(weekId));
        cookBook.setSchoolId(schoolId);
        cookBook.setSchoolName(getProfile(request, response).getSchoolName());
        cookBook.setId(schoolId + weekId);

        // 早饭
        Meal breakfast = new CookBook().new Meal();

        /* 食谱名称 */
        breakfast.setName(reqModel.breakfast.name);
        /* 对应的图片链接地址 */
        breakfast.setImages(reqModel.breakfast.images);
        cookBook.setBreakfast(breakfast);

        // 早点
        Meal breakfastCakes = new CookBook().new Meal();
        /* 食谱名称 */
        breakfastCakes.setName(reqModel.breakfastCakes.name);
        /* 对应的图片链接地址 */
        breakfastCakes.setImages(reqModel.breakfastCakes.images);
        cookBook.setBreakfastCakes(breakfastCakes);

        // 午饭
        Meal lunch = new CookBook().new Meal();
        /* 食谱名称 */
        lunch.setName(reqModel.lunch.name);
        /* 对应的图片链接地址 */
        lunch.setImages(reqModel.lunch.images);
        cookBook.setLunch(lunch);

        // 午点
        Meal lunchCakes = new CookBook().new Meal();
        /* 食谱名称 */
        lunchCakes.setImages(reqModel.lunchCakes.images);
        /* 对应的图片链接地址 */
        lunchCakes.setName(reqModel.lunchCakes.name);
        cookBook.setLunchCakes(lunchCakes);

        // 晚饭
        Meal dinner = new CookBook().new Meal();
        /* 食谱名称 */
        dinner.setName(reqModel.dinner.name);
        /* 对应的图片链接地址 */
        dinner.setImages(reqModel.dinner.images);
        cookBook.setDinner(dinner);

        yxySchManageCookbookRepository.save(cookBook);

        RespModel respModel = new RespModel();
        respModel.setMessage("成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // ID
        public String id;
        // 学校ID
        public String schoolId;
        // 学校名称
        public String schoolName;
        public Integer weekId;
        // 中文：星期一--星期七
        public String weekName;
        // 早餐
        public Meals breakfast;
        // 早点
        public Meals breakfastCakes;
        // 午餐
        public Meals lunch;
        // 午点
        public Meals lunchCakes;
        // 晚餐
        public Meals dinner;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String message;
    }
}

class Meals {
    // 食谱名称
    public String name;
    // 对应的图片链接地址
    public List<String> images;

}